# Replication Material "Voters’ expectations in constituency elections without local polls"
#####################


# Packages
library(tidyverse)
library(boot)

# Load Data
load("out/data/clean_data.rdata")

# Figure 1 =====

  # Functions
  mae <- function(x,i,true){mean(abs(true[i] - x[i]))}
  rmse <- function(x,i,true){sqrt(mean((true[i] - x[i])^2))}
  sd_fun <- function(x,i,true){sd(x[i])}
  se_sd <- function(x){
    mu4 <- mean((x -mean(x))^4)
    sd4 <- sd(x)^4
    n <- length(x)
    se2 <- sqrt(1/n * (mu4 - (n-3)/(n-1) * sd4))
    1/(2 * sd(x)) * se2
  }
  
  # Prepare Data
  df_plot <- dat_long %>% 
    group_by(group) %>%
    summarise("RMSE_pre_est"=sqrt(mean((res_erst - pre_erst)^2)),
              "RMSE_pre_se"=sqrt(mean((res_erst - pre_erst)^2))/sqrt(n()-1),
              "RMSE_pre_seboot" = sd(boot(pre_erst, rmse, 100, true=res_erst)$t),
              "RMSE_post_est"=sqrt(mean((res_erst - post_erst)^2)),
              "RMSE_post_se"=sqrt(mean((res_erst - post_erst)^2))/sqrt(n()-1),
              "RMSE_post_seboot" = sd(boot(post_erst, rmse, 100, true=res_erst)$t),
              "MAE_pre_est" = mean(abs(res_erst - pre_erst)),
              "MAE_pre_se" = sd(abs(res_erst - pre_erst))/sqrt(n()-1),
              "MAE_pre_seboot" = sd(boot(pre_erst, mae, 100, true=res_erst)$t),
              "MAE_post_est" = mean(abs(res_erst - post_erst)),
              "MAE_post_se" = sd(abs(res_erst - post_erst))/sqrt(n()-1),
              "MAE_post_seboot" = sd(boot(post_erst, mae, 100, true=res_erst)$t),
              "SD_pre_est"=sd(pre_erst),
              "SD_post_est" = sd(post_erst),
              "SD_pre_se"=se_sd(pre_erst),
              "SD_pre_seboot"=sd(boot(pre_erst, sd_fun, 100, true=res_erst)$t),
              "SD_post_se" = se_sd(post_erst),
              "SD_post_seboot"=sd(boot(post_erst, sd_fun, 100, true=res_erst)$t)) %>%
    gather(var, val,-group) %>%
    separate(var, c("stat","time","type")) %>%
    spread(type,val)
  
  # Labels
  label_names <- c(
    `MAE` = "Mean Average Error\n(MAE) \nof Expectations",
    `SD` = "Standard Deviation\nof Expectations",
    `RMSE` = "Root Mean Square Error\n(RMSE) \nof Expectations",
    "Last Election Result" = " Last Election Result " ,
    "Election Poll" = " Election Poll ",
    "Last Election Result + Election Poll" = " Last Election Result \n+ Election Poll ",
    "afd"  = "AfD",
    "cdsu" = "CDU/CSU",
    "fdp"  = "FDP",
    "green" = "Greens",
    "linke" = "Linke",
    "other" = "Other",
    "spd" = "SPD"  
  )
  
  # Significance Table
  dat_sig <- dat_long %>%
    mutate("MAE_pre"=(abs(res_erst - pre_erst)),
           "MAE_post"=(abs(res_erst - post_erst))) %>%
    group_by(group) %>%
    summarise(MAE_pval = t.test(MAE_post, MAE_pre,
                                paired = TRUE, 
                                alternative = "two.sided",na.rm=T)$p.value,
              MAE_diff = mean(MAE_post)- mean(MAE_pre),
              SD_diff = sd(post_erst) - sd(pre_erst),
              SD_pval = var.test(pre_erst, post_erst)$p.value) %>%
    gather(var, val,-group) %>%
    separate(var, c("stat","type")) %>%
    spread(type,val) %>%
    mutate(x=1.5, y=ifelse(stat=="SD",10.8,5.8),
           pval_text = case_when(
             pval <0.001 ~ "p-val. < 0.001",
             pval <0.01 ~ "p-val. < 0.01",
             pval <0.05 ~ "p-val. < 0.05",
             TRUE ~ "p-val. > 0.05"
           ))
  
  # Plot Results Figure 1
  ggplot(filter(df_plot, stat %in% c("MAE", "SD"))) +
    geom_point(aes(x=reorder(time,-est),  y=est ), 
               size=3) +
    geom_linerange(aes(x=reorder(time,-est), ymin=est-1.96*se,ymax=est+1.96*se  ), 
                   size=1) +
    geom_line(aes(x=reorder(time,-est), group=group, y=est ),linetype="dotted",size=0.5, alpha=.8) +
    geom_text(data=dat_sig, aes(x=x,y=y,group=group,
                                label=paste("Diff.",round(diff,2),"", pval_text)),
              size=2.8, col="black", alpha=0.8) +
    # facet_wrap(~stat, scales="free",ncol=2, labeller = as_labeller(label_names)) +  
    facet_grid(stat ~ group, scales="free_y", labeller = as_labeller(label_names), switch = "y") +
    xlab("") + ylab("") + 
    theme_bw()  + scale_y_continuous(position = "right") +
    theme(legend.position = "bottom",
          strip.text.y.left = element_text(angle = 0),
          legend.title = element_blank(),
          panel.grid.minor.x = element_blank(),
          panel.grid.major.x = element_blank(),
          strip.background =element_rect(fill="white"))
  
  ggsave("figure1.pdf",width=8,height=5)
